<template>
  <el-dialog
    title="分配权限"
    width="500px"
    :visible="isShow"
    @close="cancelEvent"
  >
    <el-tree
      ref="tree"
      check-strictly
      show-checkbox
      :data="treeData"
      node-key="id"
      :props="{ label: 'name' }"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button @click="cancelEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/premission'
import { changeData } from '@/utils/index'
import { sysRoleId, sysRoleAssignPrem } from '@/api/setting'
export default {
  data() {
    return {
      isShow: false,
      treeData: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysPermission()
      this.treeData = changeData(res.data, '0')
    },
    cancelEvent() {
      this.isShow = false
      this.$refs.tree.setCheckedKeys([])
    },
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('修改权限成功')
      this.cancelEvent()
    }
  }
}
</script>

<style></style>
